﻿using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;
using Skyworth.CRM.Service.Entity;

namespace Skyworth.CRM.Web.CustomerManagement
{
    public partial class CustCreditQuery : BasePage
    {
        ICustCreditBLL creditBLL = (ICustCreditBLL)IocContainer.Instance.Resolve<ICustCreditBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            SystemOverTime();
            if (!Page.IsPostBack)
            {                
                // 按钮权限控制
                //ControlAuthority(new List<LinkButton> { btnAdd, btnUpdate, btnDelete }, 3);
                LoadData();
            }
        }

        private void LoadData()
        {
            try
            {
                StringBuilder strs = new StringBuilder();
                string strWhere = "";
                if (!string.IsNullOrEmpty(this.txtCustCode.Text.Trim()))
                    strWhere += string.Format(" and tbc.FNumber like '%{0}%'", this.txtCustCode.Text.Trim());
                if (!string.IsNullOrEmpty(this.txtCustName.Text.Trim()))
                    strWhere += string.Format(" and tbc.FName like '%{0}%'", this.txtCustName.Text.Trim());
                if (ddlstType.SelectedValue != "0")
                {
                    strWhere += string.Format(" and FCreditType='{0}'", this.ddlstType.SelectedValue);
                }
                DataTable dt = creditBLL.Query(strWhere);
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dataRow in dt.Rows)
                    {
                        strs.AppendFormat("<tr rel='{0}'>", dataRow["FId"]);
                        strs.AppendFormat("<td>{0}</td><td>{1}</td><td>{2}</td>", dataRow["FCustCode"], dataRow["FCustName"], dataRow["FCreditName"]);
                        strs.AppendFormat("<td>{0}</td><td>{1}</td><td>{2}</td>", dataRow["TypeName"], dataRow["WayName"], dataRow["FAmount"]);
                        strs.AppendFormat("<td>{0}</td><td>{1}</td><td>{2}</td>", dataRow["FYearAmount"], dataRow["FBeginDate"], dataRow["FEndDate"]);
                        strs.AppendFormat("<td>{0}</td></tr>", Boolean.Parse(dataRow["FisYes"].ToString()) == true ? "生效" : "不生效");
                    }
                }
                litCreditList.Text = strs.ToString();
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void btnhidDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int Id = 0;
                int.TryParse(this.selectedCreditID.Value.Trim(), out Id);
                creditBLL.Delete(Id);
                LoadData();
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }
        }
    }
}